package wasnake.gui;


import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.event.KeyEvent;

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

import wasnake.actors.Cell;
import wasnake.actors.Food;
import wasnake.grid.Grid;
import wasnake.grid.Location;

public class GridPanel extends JPanel {
	 /** The minimum size of each cell in this panel.*/
    private static final int MIN_CELL_SIZE = 1;
    private int numRows, numCols, originRow, originCol;
    private Grid<Cell> grid;
    private int score;

    /**
     * @param layout a layout
     */
	public GridPanel(BorderLayout layout) {
		super(layout);
		numRows = 50;
		numCols = 50;
		originRow = 0;
		originCol = 0;
	}
    
	public void setGrid(Grid<Cell> grid) {
		this.grid = grid;
	}
    /**
	 * @param font2
	 *            the font currently being used
	 * @return the same font except bigger
	 */
	public Font inningFont(Font font2) {
		Font font = new Font(font2.getName(), Font.BOLD, 20);
		return font;
	}
	
	public void setScore(int score) {
		this.score = score;
	}
    
	/**
     * @return an appropriate cell size for the current size of the panel.
     */
    public int calculateCellSize() {
        return (int) Math.max(
                MIN_CELL_SIZE,
                Math.min(
                    ((getSize().getHeight()) - 50) / numRows,
                    ((getSize().getWidth()) - 50) / numCols));
    }

	/**
	 * @param cell a cell to be painted
	 * @param g the graphics where it should be painted
	 */
	public void paintCell(Cell cell, Graphics g) {
		System.out.println("painting the cell");
	     Point p = pointForLocation(cell.getLocation());
	     g.setColor(cell.getColor());
	     g.fillRect(p.x, p.y, calculateCellSize(), calculateCellSize());
	}
	
	public void test() {
	}
	
	/**
	 * @param loc a location on the grid
	 * @return the point on the panel that is equal to the location
	 */
    public Point pointForLocation(Location loc) {
        return new Point(colToXCoord(loc.getCol()) + calculateCellSize() / 2,
                rowToYCoord(loc.getRow()) + calculateCellSize() / 2);
    }

    /**
     * @param col the column of a location
     * @return the x coordinate on the panel equal to the column
     */
    protected int colToXCoord(int col) {
        return (col + 1 - originCol) * (calculateCellSize() + 1) + 1 + getInsets().left;
    }
    
    /**
     * @param row the row of a location
     * @return the y coordinate on the panel equal to the row 
     */
    protected int rowToYCoord(int row) {
        return (row + 1 - originRow) * (calculateCellSize() + 1) + 1 + getInsets().top;
    }
    
    /**
     * @return the y coordinate for where to print the score
     */
    private int getScoreYLocation() {
    	return (int) (getSize().getHeight() - 10);
    }
    
    /**
     * @return the x coordinate for where to print the score
     */
    private int getScoreXLocation() {
    	return (int)(getSize().getWidth() / 2 - 10);
    }
    
    public void gameover() {
    	JOptionPane.showMessageDialog(null, "The game is over. Your score was " + score);
    }


    /**
	 * draws the ball, paddles and strings onto the panel
	 */
	@Override
	public void paintComponent(Graphics g) {
		super.paintComponent(g);
		
			g.setColor(Color.BLACK);
			g.setFont(inningFont(g.getFont()));
			g.drawString("Score: " + score, getScoreXLocation(), getScoreYLocation());
			for(Location l: grid.getOccupiedLocations()) {
				System.out.println("checking location: " + l);
				if(grid.get(l) != null) {
					System.out.println("location not equal to null");
					paintCell((Cell) grid.get(l), g);
				}
			}
			
			for(int i = 0; i < 50; i++) { 
				paintCell(new Cell(new Food(Color.BLACK), new Location(-1, i)), g);
				paintCell(new Cell(new Food(Color.BLACK), new Location(50, i)), g);
				paintCell(new Cell(new Food(Color.BLACK), new Location(i, -1)), g);
				paintCell(new Cell(new Food(Color.BLACK), new Location(i, 50)), g);
			}
		
	}
   
}
